package com.company.app.daoImpl;

import java.util.List;

import com.company.app.dao.AbstractBaseDAO;
import com.company.app.dao.ProductDAO;
import com.company.app.entities.Product;
import com.google.code.ssm.api.InvalidateSingleCache;
import com.google.code.ssm.api.ParameterValueKeyProvider;
import com.google.code.ssm.api.ReadThroughMultiCache;
import com.google.code.ssm.api.ReadThroughMultiCacheOption;
import com.google.code.ssm.api.ReadThroughSingleCache;
import com.google.code.ssm.api.ReturnDataUpdateContent;
import com.google.code.ssm.api.UpdateSingleCache;

/**
 * The Class ProductDAOImpl.
 */
public class ProductDAOImpl extends AbstractBaseDAO<Product> implements
		ProductDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.dao.BaseInterfaceDAO#getEntityById(java.lang.Long)
	 */
	@Override
	@ReadThroughSingleCache(namespace=MEMCHACHED_NAMESPACE, expiration=60 )
	public Product getEntityById(@ParameterValueKeyProvider Long id) {
		return this.getSqlSession().selectOne(
				"com.company.app.dao.ProductMapper.getProductById", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.dao.BaseInterfaceDAO#getAllEntity()
	 */
	@Override
	public List<Product> getAllEntity() {
		return this.getSqlSession().selectList(
				"com.company.app.dao.ProductMapper.getAllProduct");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.dao.BaseInterfaceDAO#searchEntityByFilter(java.lang.Object
	 * )
	 */
	@Override
	@ReadThroughMultiCache(namespace=MEMCHACHED_NAMESPACE, expiration=180 , option=@ReadThroughMultiCacheOption(generateKeysFromResult = true, skipNullsInResult=true) )
	public List<Product> searchEntities(@ParameterValueKeyProvider List<Product> filter) {
		return this.getSqlSession().selectList(
				"com.company.app.dao.ProductMapper.getProductByFilter",	filter.get(0));
	}
	
	/* (non-Javadoc)
	 * @see com.company.app.dao.AbstractBaseDAO#updateEntity(java.lang.Object)
	 */
	@Override
	@UpdateSingleCache(namespace = MEMCHACHED_NAMESPACE, expiration = 180)
	@ReturnDataUpdateContent
	public Product updateEntity(@ParameterValueKeyProvider Product entity) {
			return super.updateEntity(entity);
	}
	
	/* (non-Javadoc)
	 * @see com.company.app.dao.AbstractBaseDAO#deleteEntity(java.lang.Object)
	 */
	@Override
	@InvalidateSingleCache(namespace = MEMCHACHED_NAMESPACE)
	public Product deleteEntity(@ParameterValueKeyProvider Product entity) {
		return super.deleteEntity(entity);
	}
	
	/* (non-Javadoc)
	 * @see com.company.app.dao.AbstractBaseDAO#createEntity(java.lang.Object)
	 */
	@Override
	@UpdateSingleCache(namespace = MEMCHACHED_NAMESPACE)
	@ReturnDataUpdateContent
	public Product createEntity(@ParameterValueKeyProvider Product entity) {
		super.createEntity(entity);
		return this.getEntityById(entity.getId());
	}

}
